﻿using CloudStorageLight.Core;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Auth.OAuth2.Requests;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Drive.v2;
using Google.Apis.Json;
using Google.Apis.Util.Store;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace CloudStorageLight.Core
{
    public class GoogleDriveUtil
    {
        public static string GoogleDriveClientId { get; set; }
        public static string GoogleDriveClientSecret { get; set; }

        public static async Task<Dictionary<string, object>> AuthorizeAsync(string path, string callbackUrl, string returnUrl, HttpContextBase ctx)
        {
            var auth = new MyAppAuthorizationCodeWebApp(path, callbackUrl, returnUrl, ctx);
            var result = await auth.AuthorizeAsync(CancellationToken.None);
            var dict = new Dictionary<string, object>();
            if (result.Credential != null)
            {
                dict["AccessToken"] = result.Credential.Token.AccessToken;
                dict["RefreshToken"] = result.Credential.Token.RefreshToken;
            }
            dict["RedirectUri"] = result.RedirectUri;
            return dict;
        }

    }

    public class MyAppAuthorizationCodeWebApp : AppAuthorizationCodeWebApp
    {

        public static AppFlowMetadata CreateAppFlowMetadata(string path, HttpContextBase ctx)
        {
            var data = new DictionaryDataStore();
            if (ctx != null)
            {
                data.Data = (Dictionary<string, object>)ctx.Session["__DcitionaryDataStore"];
                if (data.Data == null)
                {
                    data.Data = new Dictionary<string, object>();
                    ctx.Session["__DcitionaryDataStore"] = data.Data;
                }
            }

            return new AppFlowMetadata(
                  path,
                  GoogleDriveUtil.GoogleDriveClientId,
                  GoogleDriveUtil. GoogleDriveClientSecret,
                  @"/GoogleAuthCallback/IndexAsync",
                  new AppGoogleAuthorizationCodeFlow(
                     new GoogleAuthorizationCodeFlow.Initializer
                     {
                         ClientSecrets = new ClientSecrets
                         {
                             ClientId = GoogleDriveUtil.GoogleDriveClientId,
                             ClientSecret = GoogleDriveUtil.GoogleDriveClientSecret,

                         },
                         Scopes = new[] { DriveService.Scope.Drive },
                         DataStore = data,

                     }));
        }


        public MyAppAuthorizationCodeWebApp(string path, string callbackUrl, string returnUrl, HttpContextBase ctx) :
            base(path, CreateAppFlowMetadata(path, ctx), callbackUrl, returnUrl)
        {

        }
    }


}